package hos.table;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.evrencoskun.tableview.ITableView;
import com.evrencoskun.tableview.TableView;
import com.evrencoskun.tableview.adapter.AbstractTableAdapter;
import com.evrencoskun.tableview.adapter.recyclerview.holder.AbstractViewHolder;
import com.evrencoskun.tableview.listener.ITableViewListener;

import java.util.List;

import hos.table.holder.DataHolder;
import hos.table.holder.OnTableSizeListener;
import hos.table.holder.OnTitleClickListener;
import hos.table.model.Cell;
import hos.table.model.ColumnHeader;
import hos.table.model.RowHeader;


/**
 * <p>Title: TableAdapter </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/7/6 22:36
 */
public abstract class BaseTableAdapter extends AbstractTableAdapter<ColumnHeader, RowHeader,
        Cell> {

    private String title;
    private OnTitleClickListener mOnTitleClickListener;

    private OnTableSizeListener mOnTableSizeListener;


    public BaseTableAdapter() {
    }

    public BaseTableAdapter(OnTableSizeListener listener) {
        this.mOnTableSizeListener = listener;
    }

    public BaseTableAdapter(String title, OnTableSizeListener listener) {
        this.title = title;
        this.mOnTableSizeListener = listener;
    }

    public BaseTableAdapter(String title, OnTitleClickListener listener) {
        this.mOnTitleClickListener = listener;
        this.title = title;
    }

    public BaseTableAdapter(String title, OnTableSizeListener onTableSizeListener, OnTitleClickListener onTitleClickListener) {
        this.title = title;
        this.mOnTableSizeListener = onTableSizeListener;
        this.mOnTitleClickListener = onTitleClickListener;
    }

    @Override
    public void setTableView(@NonNull ITableView tableView) {
        super.setTableView(tableView);
        if (mOnTableSizeListener != null) {
            tableView.setRowHeaderWidth(mOnTableSizeListener.rowHeaderWidth());//标题宽度
        }
    }


    @Override
    public int getColumnHeaderItemViewType(int position) {
        ColumnHeader columnHeaderItem = getColumnHeaderItem(position);
        if (columnHeaderItem == null) {
            return 0;
        }
        return columnHeaderItem.getColumnHeaderItemType(position);
    }

    @Override
    public int getRowHeaderItemViewType(int position) {
        RowHeader rowHeaderItem = getRowHeaderItem(position);
        if (rowHeaderItem == null) {
            return 0;
        }
        return rowHeaderItem.getRowHeaderItemType(position);
    }

    @Override
    public int getCellItemViewType(int column) {
        Cell cellItem = getCellItem(column, 0);
        if (cellItem == null) {
            return 0;
        }
        return cellItem.getCellItemType(column);
    }

    @Override
    public void setAllItems(@Nullable List<ColumnHeader> columnHeaderItems, @Nullable List<RowHeader> rowHeaderItems, @Nullable List<List<Cell>> cellItems) {
        super.setAllItems(columnHeaderItems, rowHeaderItems, cellItems);
    }

    @Override
    public void setCellItems(@Nullable List<List<Cell>> cellItems) {
        super.setCellItems(cellItems);
    }

    @Override
    public void setColumnHeaderItems(@Nullable List<ColumnHeader> columnHeaderItems) {
        super.setColumnHeaderItems(columnHeaderItems);
    }

    @Override
    public void setRowHeaderItems(@Nullable List<RowHeader> rowHeaderItems) {
        super.setRowHeaderItems(rowHeaderItems);
    }

    public void setTableViewListener(@Nullable ITableViewListener tableViewListener) {
        ITableView tableView = getTableView();
        if (tableView instanceof TableView) {
            ((TableView) tableView).setTableViewListener(tableViewListener);
        }
    }

    public void setOnTableSizeListener(OnTableSizeListener mOnTableSizeListener) {
        this.mOnTableSizeListener = mOnTableSizeListener;
    }

    public OnTableSizeListener getOnTableSizeListener() {
        return mOnTableSizeListener;
    }

    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
        this.mOnTitleClickListener = onTitleClickListener;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void onBindRowHeaderViewHolder(@NonNull AbstractViewHolder holder, @Nullable RowHeader rowHeaderItemModel, int rowPosition) {
        if (holder instanceof DataHolder) {
            ((DataHolder) holder).onBindViewHolder(this, holder.itemView, rowHeaderItemModel, 0, rowPosition);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void onBindColumnHeaderViewHolder(@NonNull AbstractViewHolder holder, @Nullable ColumnHeader columnHeaderItemModel, int columnPosition) {
        if (holder instanceof DataHolder) {
            ((DataHolder) holder).onBindViewHolder(this, holder.itemView, columnHeaderItemModel, columnPosition, 0);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void onBindCellViewHolder(@NonNull AbstractViewHolder holder, @Nullable Cell cellItemModel, int columnPosition, int rowPosition) {
        if (holder instanceof DataHolder) {
            ((DataHolder) holder).onBindViewHolder(this, holder.itemView, cellItemModel, columnPosition, rowPosition);
        }
    }

    @NonNull
    @Override
    public View onCreateCornerView(@NonNull ViewGroup parent) {
        View view = onTitleView(parent.getContext(), getTitle());
        if (mOnTitleClickListener == null) {
            return view;
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTitleClickListener != null) {
                    mOnTitleClickListener.onClick(BaseTableAdapter.this);
                }
            }
        });
        return view;
    }

    @NonNull
    public abstract View onTitleView(Context context, String title);
}
